<template>
  <div>
    <v-header title="新增地址" :back="true"></v-header>
    <van-address-edit
      ref="edit"
      :area-list="areaList"
      show-set-default
      :address-info="info"
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      @save="onSave"
    >
      <template slot="default">
        标签：
        <van-radio-group v-model="radio">
          <van-radio name="家">家</van-radio>
          <van-radio name="公司">公司</van-radio>
          <van-radio name="学校">学校</van-radio>
        </van-radio-group>
      </template>
    </van-address-edit>
  </div>
</template>

<script>
import { areaList } from "@vant/area-data";
import axios from "axios";
export default {
  data() {
    return {
      areaList: areaList,
      radio: this.$route.query.id ? this.$route.query.label : "家",
      //表单数据
      info: {
        ...this.$route.query,
        isDefault: this.$route.query.isDefault == "true" ? true : false,
      },
    };
  },
  methods: {
    //1.点击了保存
    // onSave(e) {
    //   console.log(e);
    //   if (this.$route.query.id) {
    //     axios({
    //       url: "/api/addressedit",
    //       method: "post",
    //       data: {
    //         id: this.$route.query.id,
    //         username: e.name,
    //         userphone: e.tel,
    //         regioncode: e.areaCode,
    //         location: e.province + e.city + e.county,
    //         useraddress: e.addressDetail,
    //         status: e.isDefault ? 1 : 0,
    //         label: this.radio,
    //       },
    //     }).then((res) => {
    //       if (res.data.code == 200) {
    //         //弹信息
    //         alert(res.data.msg);
    //         //跳转list
    //         this.$router.push("/dizhiguanli");
    //       }
    //     });
    //     //编辑
    //   } else {
    //     //添加
    //     // {"name":"haotingting","tel":"15800000000","country":"","province":"山西省","city":"太原市","county":"小店区","areaCode":"140105","postalCode":"","addressDetail":"123hao ","isDefault":true}
    //     axios({
    //       url: "/api/addressadd",
    //       method: "post",
    //       data: {
    //         uid: JSON.parse(localStorage.getItem("userInfo")).uid,
    //         username: e.name,
    //         userphone: e.tel,
    //         regioncode: e.areaCode,
    //         location: e.province + e.city + e.county,
    //         useraddress: e.addressDetail,
    //         status: e.isDefault ? 1 : 0,
    //         label: this.radio,
    //       },
    //     }).then((res) => {
    //       if (res.data.code == 200) {
    //         //弹信息
    //         aleat(res.data.msg);
    //         //跳转list
    //         this.$router.push("/dizhiguanli");
    //       }
    //     });
    //   }
    // },
    onSave(e) {
      console.log(JSON.stringify(e));
      axios({
        url: "/api/addressadd",
        method: "post",
        data: {
          //{"name":"11","tel":"11111111111","country":"","province":"北京市","city":"北京市","county":"东城区","areaCode":"110101","postalCode":"","addressDetail":"1","isDefault":true}
          uid: JSON.parse(localStorage.getItem("userInfo")).uid,
          username: e.name,
          userphone: e.tel,
          regioncode: e.areaCode,
          location: e.province + e.city + e.county,
          useraddress: e.addressDetail,
          status: e.isDefault ? 1 : 0,
          label: this.radio,
        },
        headers:{
          authorization:JSON.parse(localStorage.getItem("userInfo")).token
        }
      }).then((res) => {
        if (res.data.code == 200) {
          //弹信息
          alert(res.data.msg);
          //跳转list
          this.$router.push("/dizhiguanli");
        }else{
          alert("失败")
        }
      });
    },
  },
};
</script>

<style scoped>
/* .van-radio-group{
    float: left;
} */
.van-radio {
  float: left;
}
</style>